package im.zego.zimexample.ui.room.adapter;

import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;
import java.util.Vector;

import im.zego.zimexample.R;
import im.zego.zimexample.entity.UserInfo;


/**
 * Created by zego on 2018/2/6.
 */

public class MemberListAdapter extends RecyclerView.Adapter {

    private List<UserInfo> userInfoList = new Vector<>();

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_member_list, parent, false);
        MyViewHolder myViewHolder = new MyViewHolder(v);
        return myViewHolder;
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        final MyViewHolder myViewHolder = (MyViewHolder) holder;
        myViewHolder.itemView.setTag(position);
        myViewHolder.title.setText(userInfoList.get(position).getSessionID());
    }

    @Override
    public int getItemCount() {
        return userInfoList.size();
    }

    /**
     * 新增模块信息
     */
    public boolean addSessionList(UserInfo info) {
        boolean isExist = false;
        for (UserInfo userInfo : userInfoList) {
            if (info.getSessionID() != null && info.getSessionID().equals(userInfo.getSessionID())) {
                isExist = true;
            }
        }
        if (!isExist) {
            userInfoList.add(info);
        }
        return !isExist;
    }

    public void clear() {
        userInfoList.clear();
        notifyDataSetChanged();
    }

    public void delete(String userID) {

        for (UserInfo userInfo : userInfoList) {
            if (userInfo.getSessionID().equals(userID)) {
                userInfoList.remove(userInfo);
            }
        }
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {
        TextView title;

        MyViewHolder(View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.title);
        }
    }

}

